# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
'''
外键冲突未解决
'''
'''
姚明 281720252@qq.com
'''


class DeclarAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryDeclar(self, index):  # zzh_declar表查询记录
        """
        申报车和item表综合查询
        :param page_index:
        :return: json格式的item表和declar表的综合数据
        """
        cur = self.conn.cursor()
        sql = "select * from zzh_declar dc, zzh_item it WHERE user_id=%s AND dc.item_id=it.item_id"
        total_count = cur.execute(sql, [index])
        declarList = cur.fetchall()
        declarListJson = Table2Json.declar_and_item_to_json(declarList)
        cur.close()
        self.conn.close()
        return total_count, declarListJson

        # if (page_index * PAGE_SIZE) < total_count:
        #     start = (page_index - 1) * PAGE_SIZE
        #     end = page_index * PAGE_SIZE
        #     declarListPart = declarList[start: end]
        #
        #     declarListJson = Table2Json.declar_and_item_to_json(declarListPart)
        #     return total_count, declarListJson
        # else:
        #     start = (page_index - 1) * PAGE_SIZE
        #     declarListPart = declarList[start:]
        #     declarListJson = Table2Json.declar_and_item_to_json(declarListPart)
        #     return total_count, declarListJson

    def addDeclar(self, addDeclar):  # process表添加记录
        itemId = addDeclar['itemId']
        userId = addDeclar['userId']
        declarStatus = addDeclar['declarStatus']
        serviceId = addDeclar['serviceId']
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_declar (item_id,user_id,declar_status,service_id) values (%s,%s,%s,%s)"
        '''
            外键冲突使用异常捕获，直接返回错误信息
       '''
        addDeclarResultOfDatabase = cur.execute(sql, [itemId, userId, declarStatus, serviceId])
        self.conn.commit()
        if addDeclarResultOfDatabase:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateDeclarById(self, newUpdateDeclar, declarId):
        itemId = newUpdateDeclar['itemId']
        userId = newUpdateDeclar['userId']
        declarStatus = newUpdateDeclar['declarStatus']
        serviceId = newUpdateDeclar['serviceId']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_declar SET item_id=%s,user_id=%s,declar_status=%s,service_id=%s WHERE declar_id=%s"
        updateDeclarResultOfDatabase = cur.execute(sql, [itemId, userId, declarStatus, serviceId, declarId])
        self.conn.commit()
        if updateDeclarResultOfDatabase:  # 更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteDeclarById(self, declarId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_declar WHERE declar_id=%s"
        deleteDeclarResultOfDatabase = cur.execute(sql, [declarId])
        self.conn.commit()
        if deleteDeclarResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def delete_declars(self, declar_ids):
        cur = self.conn.cursor()
        sql = "delete from zzh_declar WHERE declar_id=%s"
        for ids in declar_ids:
            cur.execute(sql, [ids])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return

    def queryDeclarInfo(self, userId, itemId):
        cur = self.conn.cursor()
        sql = "select declar_id,declar_status from zzh_declar WHERE user_id=%s AND item_id=%s"
        rs = cur.execute(sql, [userId, itemId])
        declarIdAndDeclarStatus = cur.fetchall()
        declarIdAndDeclarStatusJSON = Table2Json.declarIdAndStatus_list_to_json(declarIdAndDeclarStatus)
        if rs:
            return declarIdAndDeclarStatusJSON
        else:
            return None
